<?php
/**
* @package phpBB3 ATK - Admin Tool Kit
* @version $Id: atk_phpbb.php 186 2009-01-10 22:32:51Z ej.frerejean $
* @copyright (c) 2008 Admin Tool Kit community development team
* @author Erik Frèrerejean (Erik Frèrejean)
* @author Nathon Guse (EXreaction)
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
* @ignore
*/
if (!defined('IN_ATK') || !defined('IN_PHPBB'))
{
	exit;
}

/**
* A class to implement some phpBB functions we need, but of which we can't use the 
* normal implementation
*/
class atk_phpbb
{
	/**
	* Add a phpBB language file
	*
	* @param String $lang_file The name of the language file.
	*/
	function add_phpbb_lang($lang_file)
	{
		global $user;
		
		// Switch language path
		$user->set_custom_lang_path(PHPBB_LANGUAGE_PATH);
		
		// Add the file
		$this->atk_add_lang($lang_file);
		
		// Switch back
		$user->set_custom_lang_path(ATK_LANGUAGE_PATH);
	}
	
	/**
	* Fallback save wrapper for $user->add_lang.
	* This method will try to load a language file, but takes the following steps
	*	Test the existance of the file in the users language
	*	Test the existance of the file in the boards default language
	*	Test the existance of the file in the "en" language
	* So we'll always be able to include a file.
	*
	* @param String $lang_file The name of the language file.
	*/
	function atk_add_lang($lang_file)
	{
		global $config, $user;
		
		static $old_lang_name = '';
		if (empty($old_lang_name))
		{
			$old_lang_name = $user->lang_name;
		}
		
		// Update the $user->lang_name to the correct lang path
		if (!file_exists($user->lang_path . $user->lang_name . '/' . $lang_file . '.' . PHP_EXT))
		{
			if ($config['default_lang'] != $user->lang_name && file_exists($user->lang_path . $config['default_lang'] . '/' . $lang_file . '.' . PHP_EXT))
			{
				$user->lang_name = $config['default_lang'];
			}
			else if ($config['default_lang'] != 'en')
			{
				$user->lang_name = 'en';
			}
			else
			{
				// Shouldn't happen but if so trigger an error
				trigger_error ('The requested language file {' . $lang_file . '} doesn\'t exists!', E_USER_ERROR);
			}
		}
		
		// Add the file
		$user->add_lang($lang_file);
		
		// Restore lang_name
		$user->lang_name = $old_lang_name;
	}
	
	/**
	* Own implementation of page_footer
	*/
	function page_footer()
	{
		global $template;
		
		// Parse the page
		$template->display('atk_body');

		garbage_collection();
		exit_handler();
	}
	
	/**
	* Own implementation of page_header
	*
	* @param String $html_file The name of the html that should be used for this page
	* @param String $page_title The title of the requrested page
	*/
	function page_header($html_file, $page_title)
	{
		global $template;
		
		// Strip the file extentions of the file name if given
		if (false !== ($pos = strpos($html_file, '.')))
		{
			$html_file = substr($html_file, 0, $pos);
		}
		
		// Set the template file
		$template->set_filenames(array(
			'atk_body' => $html_file . '.html')
		);

		// Assign a bunch of common vars
		$template->assign_vars(array(
			'PAGE_TITLE'	=> get_lang_string($page_title),
			'PAGE_EXPLAIN'	=> get_lang_string($page_title . '_EXPLAIN'),
		));
	}
}
?>